﻿/*************************************************************************************
 * 文 件 名:   HK_SingleChoiceItem
 * 
 * 描    述:   单选题模板类
 * 
 * 版    本：  V1.0
 * 创 建 者：  京产肠饭
 * 创建时间：  2021/11/18
 * 链   接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace HKTools
{
    public class HK_SingleChoiceItem : HK_QuesItem
    {
        public Toggle optionA_Toggle;
        Text optionA_Lable;
        public Toggle optionB_Toggle;
        Text optionB_Lable;
        public Toggle optionC_Toggle;
        Text optionC_Lable;
        public Toggle optionD_Toggle;
        Text optionD_Lable;

        List<GameObject> optionBG_List = new List<GameObject>();

        List<Toggle> option_List = new List<Toggle>();

        int answerID;

        public void InitQuesItem(HK_SingleChoiceData quesData, int id)
        {
            ID_Txt.text = id.ToString() + "、";

            string answerIDStr = quesData.Answer.ToUpper().Trim();

            switch (answerIDStr)
            {
                case "A":
                    answerID = 0;
                    break;
                case "B":
                    answerID = 1;
                    break;
                case "C":
                    answerID = 2;
                    break;
                case "D":
                    answerID = 3;
                    break;
            }

            option_List.Add(optionA_Toggle);
            option_List.Add(optionB_Toggle);
            option_List.Add(optionC_Toggle);
            option_List.Add(optionD_Toggle);

            optionA_Toggle.isOn = false;
            optionB_Toggle.isOn = false;
            optionC_Toggle.isOn = false;
            optionD_Toggle.isOn = false;

            optionA_Lable = optionA_Toggle.transform.Find("Label").GetComponent<Text>();
            optionB_Lable = optionB_Toggle.transform.Find("Label").GetComponent<Text>();
            optionC_Lable = optionC_Toggle.transform.Find("Label").GetComponent<Text>();
            optionD_Lable = optionD_Toggle.transform.Find("Label").GetComponent<Text>();

            optionBG_List.Add(optionA_Toggle.transform.Find("Background").gameObject);
            optionBG_List.Add(optionB_Toggle.transform.Find("Background").gameObject);
            optionBG_List.Add(optionC_Toggle.transform.Find("Background").gameObject);
            optionBG_List.Add(optionD_Toggle.transform.Find("Background").gameObject);

            quesName_Txt.text = quesData.QuesName;

            optionA_Lable.text = quesData.Options[0];
            optionB_Lable.text = quesData.Options[1];
            optionC_Lable.text = quesData.Options[2];
            optionD_Lable.text = quesData.Options[3];

            analysis_Txt.text = quesData.Analysis;

            optionA_Toggle.onValueChanged.AddListener(OnSelectToggleA);
            optionB_Toggle.onValueChanged.AddListener(OnSelectToggleB);
            optionC_Toggle.onValueChanged.AddListener(OnSelectToggleC);
            optionD_Toggle.onValueChanged.AddListener(OnSelectToggleD);
        }

        void OnSelectToggleA(bool isOn)
        {
            if (!isOn)
                return;

            isCorrect = answerID == 0 ? true : false;
        }

        void OnSelectToggleB(bool isOn)
        {
            if (!isOn)
                return;

            isCorrect = answerID == 1 ? true : false;
        }

        void OnSelectToggleC(bool isOn)
        {
            if (!isOn)
                return;

            isCorrect = answerID == 2 ? true : false;
        }

        void OnSelectToggleD(bool isOn)
        {
            if (!isOn)
                return;

            isCorrect = answerID == 3 ? true : false;
        }

        public void HideToggle()
        {
            for (int i = 0; i < optionBG_List.Count; i++)
            {
                optionBG_List[i].SetActive(false);
            }
        }

        public override bool IsCorrect()
        {
            return isCorrect;
        }

        public override bool IsDone()
        {
            isDone = false;

            for (int i = 0; i < option_List.Count; i++)
            {
                if (option_List[i].isOn)
                    isDone = true;
            }

            return isDone;
        }
    }
}